<div id="app">
  <el-dialog
    :title="!houses.username ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="houses" :rules="dataRule" ref="houses" @keyup.enter.native="housesSubmit()" label-width="80px">
    <el-form-item label="手机号" prop="phone">
      <el-input v-model="houses.phone" placeholder="手机号"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="pwd">
      <el-input v-model="houses.pwd" placeholder="密码"></el-input>
    </el-form-item>
    <el-form-item label="权限" prop="status">
      <el-input v-model="houses.status" placeholder="权限"></el-input>
    </el-form-item>
    <el-form-item label="最后登录时间" prop="lastonline">
      <el-input v-model="houses.lastonline" placeholder="最后登录时间"></el-input>
    </el-form-item>
    <el-form-item label="最后登录IP" prop="lastip">
      <el-input v-model="houses.lastip" placeholder="最后登录IP"></el-input>
    </el-form-item>
    <el-form-item label="状态" prop="unonline">
      <el-input v-model="houses.unonline" placeholder="状态"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="housesSubmit()">确定</el-button>
    </span>
  </el-dialog>
</div>

<script>
  new Vue ({
    el:"#app",
    data () {
      return {
        baseUrl:"http://localhost:88/api",
        visible: false,
        houses: {
          username: 0,
          phone: '',
          pwd: '',
          status: '',
          lastonline: '',
          lastip: '',
          unonline: ''
        },
        dataRule: {
          phone: [
            { required: true, message: '手机号不能为空', trigger: 'blur' }
          ],
          pwd: [
            { required: true, message: '密码不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '权限不能为空', trigger: 'blur' }
          ],
          lastonline: [
            { required: true, message: '最后登录时间不能为空', trigger: 'blur' }
          ],
          lastip: [
            { required: true, message: '最后登录IP不能为空', trigger: 'blur' }
          ],
          unonline: [
            { required: true, message: '状态不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.houses.username = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['houses'].resetFields()
          if (this.houses.username) {
            axios.post(this.baseUrl+`/auth.findhouse.com/admin/info/${this.houses.username}`).then(({data}) => {
              if (data && data.code === 0) {
                this.houses.phone = data.admin.phone
                this.houses.pwd = data.admin.pwd
                this.houses.status = data.admin.status
                this.houses.lastonline = data.admin.lastonline
                this.houses.lastip = data.admin.lastip
                this.houses.unonline = data.admin.unonline
              }
            })
          }
        })
      },
      // 表单提交
      housesSubmit () {
        this.$refs['houses'].validate((valid) => {
          if (valid) {
            axios.post(this.baseUrl+`/auth.findhouse.com/admin/${!this.houses.username ? 'save' : 'update'}`,this.houses).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  })
</script>
